/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.core.internal.diff;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * @author liao
 */
public class JsonReadUtil {
	public static String readJsonToString(String filePath) {
		String jsonStr = "";
		try {
			Reader reader = new InputStreamReader(new FileInputStream(filePath),
					StandardCharsets.UTF_8);
			try {
				int ch = 0;
				StringBuilder sb = new StringBuilder();
				while ((ch = reader.read()) != -1)
					sb.append((char) ch);
				jsonStr = sb.toString();
				String str = jsonStr;
				reader.close();
				return str;
			} catch (Throwable throwable) {
				try {
					reader.close();
				} catch (Throwable throwable1) {
					throwable.addSuppressed(throwable1);
				}
				throw throwable;
			}
		} catch (Exception e) {
			return null;
		}
	}

	public static void main(String[] args) {
		// readJsonToString("/Users/rayduan/jacoco/a.json");
		String classList = readJsonToString(
				"D:\\workblue\\jacoco\\lib-ray\\report\\diff\\code-diff-test.json");
		System.out.println(classList);
		List<ClassInfoDto> classInfos = new Gson().fromJson(classList,
				(new TypeToken<List<ClassInfoDto>>() {
				}).getType());
		System.out.println(new Gson().toJson(classInfos));
	}
}
